﻿@model SCM.Model.DBEntity.T_Customer
@{
    Layout = null;
}
@{
    var contactList = ViewBag.ContactList;
}
<div class="home-desktop">
    <div class="content-pages-title">
        <h1>编辑客户</h1>
    </div>
    <div class="content-notice-body">
        <!--客户基本信息-->
        <div class="archives-info" style="display: block;">
            <form action="@(Url.Action("Modify"))" method="post" id="form-edit-basicCustomer">
                <table style="width: 100%; border: none;">
                    <tr>
                        <td valign="top">
                            <div class="notice-box-left">
                                <div class="modular-info" style="display: block;">
                                    <ul class="form">
                                        <li>
                                            <em>所属区域</em>
                                            <input type="text" value="@Model.CustomerArea" name="customerArea" maxlength="200" />
                                        </li>
                                        <li>
                                            <em>客户名称</em>
                                            <input type="text" value="@Model.CustomerName" name="customerName" maxlength="100" required />
                                        </li>
                                        <li>
                                            <em>短名</em>
                                            <input type="text" value="@Model.ShortName" name="shortName" maxlength="100" required />
                                        </li>
                                        <li>
                                            <em>纳税人识别号</em>
                                            <input type="text" value="@Model.IdentityNumber" name="identityNumber" maxlength="50" required />
                                        </li>
                                        <li>
                                            <em>绿色认证状态</em>
                                            <select asp-items="@Html.GetEnumItemList(typeof(GreenAVLStatusEnum))" asp-for="GreenAVLStatus"></select>
                                        </li>
                                        <li>
                                            <em>使用状态</em>
                                            <select asp-items="@Html.GetEnumItemList(typeof(UseStatusEnum))" asp-for="UseStatus"></select>
                                        </li>
                                        <li class="row-last">
                                            <em>&nbsp;</em>&nbsp;&nbsp;
                                            <button type="button" class="cotrlBtn btnBlue" id="btn_save">保存</button>
                                        </li>
                                    </ul>
                                    <input type="hidden" id="hdCustomerId" name="id" value="@Model.ID" />
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <!--客户联系人列表-->
        <div class="archives-modular" id="contacterList" data-tmp-id="d" style="margin-top:8px;">
            <form id="form-contacterList" action="@Url.Action("SaveContacterList")" method="post" enctype="multipart/form-data">
                <div class="modular-title"><h2>客户联系人</h2></div>
                <div class="modular-info" style="display: block;">
                    <div style="padding-right: 10px;">
                        <table class="job-item-table" id="tb_customer">
                            <thead>
                                <tr>
                                    <th>联系人</th>
                                    <th>部门</th>
                                    <th>职位</th>
                                    <th>电话</th>
                                    <th>邮箱</th>
                                </tr>
                            </thead>
                            <tbody>
                                @if (contactList != null && contactList.Count > 0)
                                {
                                    foreach (var item in contactList)
                                    {
                                        <tr>
                                            <td><input type="hidden" name="id" value="@item.ID" class="zyclass" /><input type="text" name="contacter" value="@item.Contacter" maxlengths="50" class="input-title"></td>
                                            <td><input type="text" name="department" value="@item.Department" maxlengths="100" class="input-title"></td>
                                            <td><input type="text" name="title" value="@item.Title" maxlengths="100" class="input-title"></td>
                                            <td><input type="text" name="telPhone" value="@item.TelPhone" maxlengths="20" class="input-title"></td>
                                            <td><input type="text" name="email" value="@item.Email" maxlengths="100" class="input-title"><a href="javascript:void(0);" class="itemclose" title="删除"><i class="icon-del"></i></a></td>
                                        </tr>
                                    }
                                }
                                <tr class="last">
                                    <td colspan="5" style="text-align: center;">
                                        <button type="button" class="cotrlBtn btnDefault left add-table-item"><i class="icon-add"></i>添加</button>
                                        <button type="button" class="cotrlBtn btnBlue" id="btnSaveContracter">保存</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/html" id="tmp-d">
    <!--模版d-->
    <tr>
        <td style="min-width:150px;"><input type="hidden" class="zyclass" name="id" /><input type="text" name="contacter" maxlengths="50" class="input-title"></td>'
        <td><input type="text" name="department" maxlengths="100" class="input-title"></td>'
        <td><input type="text" name="title" maxlengths="100" class="input-title"></td>'
        <td><input type="text" name="telPhone" maxlengths="20" class="input-title"></td>'
        <td><input type="text" name="email" maxlengths="100" class="input-title"><a href="javascript:void(0);" class="itemclose" title="删除"><i class="icon-del"></i></a></td>
    </tr>
</script>
<script type="text/javascript">
    //保存基本信息
    $("#btn_save").click(function () {
        if ($('#form-edit-basicCustomer').valid()) {
            $("#form-edit-basicCustomer").ajaxSubmit({
                datatype: "json",
                data: $('#form-edit-basicCustomer').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        $("#hdCustomerId").val(result.data);
                        dcAlert(result.message, 0, 1, function () {
                            //$(".layer-dialog-close-btn").click();
                            //刷新显示数据
                            btn_search_click();
                        });
                    }
                    else {
                        dcAlert(result.message, 0, 2);
                    }
                }
            });
        }
    });

    //保存客户联系人
    $("#btnSaveContracter").click(function () {
        var customerId = $("#hdCustomerId").val();
        if (customerId == null || customerId == "") {
            dcAlert("请添加基础信息", 0, 2);
            return;
        }

        var errorMsg = "";
        var contactList = new Array();

        //校验数据，生成提交数据集合
        $("#tb_customer >tbody>tr:not(.last)").each(function () {
            var tdList = $(this).children();
            //var id = tdList.eq(0).find('input[name="zyclass"]').val();
            //var contacter = tdList.eq(0).find('input[name="contacter"]').val();

            var id = tdList.eq(0).find('input[type="hidden"]').val();
            var contacter = tdList.eq(0).find('input[type="text"]').val();
            var department = tdList.eq(1).find("input").val();
            var title = tdList.eq(2).find("input").val();
            var telPhone = tdList.eq(3).find("input").val();
            var email = tdList.eq(4).find("input").val();

            if (contacter == null || contacter == "") {
                errorMsg = "请添加联系人";
                tdList.eq(0).find('input[type="text"]').focus();
                return false;
            }
            if (department == null || department == "") {
                errorMsg = "请添加部门";
                tdList.eq(1).find("input").focus();
                return false;
            }
            if (title == null || title == "") {
                errorMsg = "请添加职位";
                tdList.eq(2).find("input").focus();
                return false;
            }
            if (telPhone == null || telPhone == "") {
                errorMsg = "请添加电话";
                tdList.eq(3).find("input").focus();
                return false;
            }
            if (email == null || email == "") {
                errorMsg = "请添加邮箱";
                tdList.eq(4).find("input").focus();
                return false;
            }

            contactList.push({
                id: id,
                customerId, customerId,
                contacter: contacter,
                department: department,
                title: title,
                telPhone: telPhone,
                email: email
            });
        });

        if (errorMsg != "") {
            dcAlert(errorMsg, 0, 2);
            return;
        }

        if ($("#form-contacterList").valid()) {
            $("#form-contacterList").ajaxSubmit({
                datatype: "json",
                data: { customerId:customerId,contactList: contactList },//$('#form-contacterList').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        dcAlert(result.message, 0, 1, function () {
                            if (result.data != "") {
                                //添加后让页面name为id的隐藏域有值
                                var idList = result.data.split(",");
                                $(".zyclass").each(function (index, element) {
                                    $(this).val(idList[index]);
                                });
                            }
                            //刷新显示数据
                            btn_search_click();
                        });
                    }
                    else {
                        dcAlert(result.message, 0, 2);
                    }
                }
            });
        }
    });
</script>
